﻿@using SalePromotion.Extensions
@using SalePromotion.Models
@using SalePromotion.Service
@using eBay.Service.Core.Soap
@model SalePromotion.Models.SaleModel
@{
    ViewBag.Title = "SaleEdit";

}
<h2>
    @(Model.Id.HasValue && Model.Id > 0 ? "Update Existing Sale" : "Create a New Sale")
    
</h2>
@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.Id)
    @Html.HiddenFor(m => m.SelectedListings)
    @Html.ValidationSummary(true, "sale creation or update was unsuccessful. Please correct the errors and try again.")
    <div>
        <fieldset>
            <legend>Choose a name and dates for your sale</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.SaleName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.SaleName)
                @Html.ValidationMessageFor(m => m.SaleName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.StartDateDate)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.StartDateDate, new {@class = "middleWidthText"})
                @Html.ValidationMessageFor(m => m.StartDateDate)
                @Html.DropDownListFor(i => i.StartDateHour, Model.Hours, new {@class = "smallWidthText"})
                @Html.DropDownListFor(i => i.StartDateMinute, Model.Minutes, new {@class = "smallWidthText"})
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.EndDateDate)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.EndDateDate, new {@class = "middleWidthText"})
                @Html.ValidationMessageFor(m => m.EndDateDate)
                @Html.DropDownListFor(i => i.EndDateHour, Model.Hours, new {@class = "smallWidthText"})
                @Html.DropDownListFor(i => i.EndDateMinute, Model.Minutes, new {@class = "smallWidthText"})
            </div>
        </fieldset>
        <fieldset>
            <legend>Choose your discount</legend>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(i => i.IsAuctionExcluded) <span>Price discount (auctions excluded)</span>
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.RadioButtonFor(i => i.DiscountType, (int) DiscountCodeType.Percentage) <span>Offer
                                                                                                 a percentage off the original price </span>
                @Html.TextBoxFor(m => m.DiscountRatioByPercent, new {@class = "smallWidthText"})<span>%</span>
                @Html.ValidationMessageFor(m => m.DiscountRatioByPercent)
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.RadioButtonFor(i => i.DiscountType, (int) DiscountCodeType.Price, new {})
                <span>Discount the original price by a set amount </span>
                @Html.TextBoxFor(m => m.DiscountRatioFixed, new {@class = "smallWidthText"})
                @Html.ValidationMessageFor(m => m.DiscountRatioFixed)
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(i => i.IsFreeShipping) <span>Free Shipping (applies to the first U.S.
                                                             shipping service)</span>
            </div>
        </fieldset>
        <fieldset>
            <legend>Choose your listings</legend>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(i => i.AllStoreInventoryItems) <span>Add all listings from a category
                                                                 </span>@Html.DropDownListFor(i => i.StoreCategoryId, Model.StoreCategories)
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(i => i.AllFixedPriceItems) <span>Add all active Fixed Price listings
                                                             </span>
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(i => i.AllAuctionItems) <span>Add all active Auction-style listings
                                                          </span><span>Only free shipping can be selected for auction listings. </span>
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
        @*         @Html.ActionLink("Select listings individually", "ChooseListing", new { Model.Id })*@
                <a href="#" id="chooseListing">Select listings individually</a>
                <input type="submit" name="Cancel" disableValidation="true"  style="display: none" />

                @if (Model.Id.HasValue && Model.Id > 0)
                {
                    <span>&nbsp;|&nbsp;</span>
                    <a href="#" id="clearListings">clear Listings</a>
                }
            </div>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                <span id="listingNo">@Model.TotalSelectedListings listings have been added to the sale. </span>
            </div>
        </fieldset>
        <fieldset>
            <div class="editor-label">
                <label>
                </label>
            </div>
            <div class="editor-field">
                <input type="submit" value='@(Model.Id.HasValue && Model.Id > 0 ? "Save Changes" : "Create your sale")' class="btn" />|
                @Html.ActionLink("Cancel", "Index")
            </div>
        </fieldset>
    </div>
    <div id="ConfirmMessage" title="Confirm Clear Listings" style="display: none">
        <p id="ConfirmMessageBody" class="msgbody">
            All listings associated with this sale will be removed from this sale. Are you sure
            you want to clear all listings?</p>
    </div>
    <div id="listingContainer" style="display: none">
        @*  @Html.Partial("~/Views/Home/ChooseListing.cshtml", new EBayListingModel())*@
    </div>
}
@section Scripts{
    <script type="text/javascript">
        var auctionExcluded = function () {
            $("#AllAuctionItems").attr("disabled", true);

            $("input[name='DiscountType']").each(function () {
                $(this).removeAttr("disabled");
            });

            $("#DiscountRatioFixed").removeAttr("disabled");
            $("#DiscountRatioByPercent").removeAttr("disabled");
        };
        var auctionNotExcluded = function () {

            $('input[name="DiscountType"]').each(function () {
                $(this).attr("disabled", true);
            });
            $("#DiscountRatioFixed").attr("disabled", true);
            $("#DiscountRatioByPercent").attr("disabled", true);

            $("#AllAuctionItems").removeAttr("disabled");
        };
        $(function () {



            if ($("#IsAuctionExcluded").attr("checked") == true) {
                auctionExcluded();
            }

            else {

                auctionNotExcluded();

            }

            $("#IsAuctionExcluded").click(function () {
                if ($(this).attr("checked") == true) {
                    auctionExcluded();

                } else {
                    {
                        auctionNotExcluded();
                    }
                }
            });
            $("#StartDateDate").datepicker(); //"option", "dateFormat", "mm/dd/yy");
            $("#EndDateDate").datepicker(); //"option", "dateFormat", "mm/dd/yy");
            $("#StartDateDate").datepicker("option", "dateFormat", "mm/dd/yy");
            $("#EndDateDate").datepicker("option", "dateFormat", "mm/dd/yy");

            $("#listingContainer").dialog(
                     {
                         modal: true,
                         width: "auto",
                         height: "auto",
                         autoOpen: false,
                         buttons: {
                             "Ok": function () {
                                 $(this).dialog('close');
                             },
                             "Cancel": function () {
                                 $(this).dialog('close');
                             }
                         }
                     });

            $("#chooseListing").click(function () {
                //  $("#listingContainer").dialog("open");
                $(this).next().click();

            });

            $("#clearListings").click(function () {
                $('#ConfirmMessage').dialog('open');
            });


            $('#ConfirmMessage').dialog({
                autoOpen: false,
                width: 300,
                modal: true,
                buttons: {
               
                    "OK": function () {
                        $(this).dialog('close');
                        $("span#listingNo").hide();
                        $("input#SelectedListings").val("");
                    },
                         "Cancel": function () {
                        $(this).dialog('close');
                    }
                }
            });



        });
    </script>
}
